import QtQuick 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4

import com.yihu.xchat.model 1.0

import "../widgets" as Widgets
import "../styles" as Styles

import "../../js/dialog/LoginDialog.js" as Js

// 登录对话框
Window {
    id: loginDialog

    maximumWidth: 250
    maximumHeight: 370
    minimumWidth: 250
    minimumHeight: 370
    visible: true
    title: "登录"

    // 槽
    readonly property var onSignInSucceed: Js.onSignInSucceed
    readonly property var onSignInFailed: Js.onSignInFailed

    // 消息对话框
    MessageDialog {
        id: messageDialog
        visible: false
        title: ""
        text: ""
        onAccepted: {
            Js.resumeUI();
        }
    }

    ColumnLayout {
        id: topLayout
        anchors.fill: parent
        anchors.topMargin: 0
        anchors.leftMargin: 30
        anchors.rightMargin: 30
        anchors.bottomMargin: 10

        // 头像
        Widgets.RoundImage {
            id: avatar

            anchors.horizontalCenter: parent.horizontalCenter

            width: 100
            height: 100
            source: "qrc:/images/avatar_default.png"
        }

        RowLayout {
            id: userNameLayout
            anchors.top: avatar.bottom
            anchors.topMargin: 30

            TextField {
                id: userNameField
                text: "sand"
                Layout.fillWidth: true
                placeholderText: "用户名"

                style: flatTextField
            }
        }

        RowLayout {
            id: passwordLayout
            anchors.top: userNameLayout.bottom
            anchors.topMargin: 5

            TextField {
                id: passwordField
                text: "sand"
                Layout.fillWidth: true
                placeholderText: "密码"
                echoMode: TextInput.Password
                focus: true

                style: flatTextField

                Keys.onPressed: {
                    if(Qt.Key_Return == event.key || Qt.Key_Enter == event.key){
                        Js.signIn();
                    }
                }
            }
        }

        RowLayout {
            anchors.top: passwordLayout.bottom
            anchors.topMargin: 5
            anchors.horizontalCenter: parent.Center
            width: parent.width

            Button {
                id: statusField
                text: "Status"
                style: flatButton
            }

            Item {
                Layout.fillWidth: true
            }

            Button {
                id: signInBtn
                text: "Sign In"
                style: flatButton

                onClicked: {
                    Js.signIn();
                }
            }
        }

        // 分隔线
        ColumnLayout {
            Text {
                id: info
                Layout.fillWidth: true
                width: parent.width
                color: "red"
            }

            Rectangle {
                Layout.fillWidth: true
                width: parent.width
                height: 2
                border.width: 2
                border.color: "gray"
            }
        }

        RowLayout {
            anchors.left: parent.left
            anchors.right: parent.right
            Layout.fillWidth: true

            CheckBox {
                id: rememberPasswordBtn
                text: "保存密码"
                checked: true
            }

            Button {
                id: registerBtn
                text: "注册"
                anchors.right: parent.right

                style: flatButton
            }
        }

        RowLayout{
            anchors.left: parent.left
            anchors.right: parent.right
            Layout.fillWidth: true

            CheckBox {
                id: autoLoginBtn
                text: "自动登录"

                onCheckedChanged: {
                    if(checked) rememberPasswordBtn.checked = true;
                }
            }

            Button {
                id: forgetPasswordBtn
                text: "忘记密码"
                anchors.right: parent.right

                style: flatButton
            }
        }
    }

    // 文本样式
    Styles.FlatTextFieldStyle {
        id: flatTextField
    }

    // 按钮样式
    Styles.FlatButtonStyle{
        id: flatButton
    }

    Timer {id: timer}

    Component.onCompleted: {
        if(ConfigSingleton.autoLogin){
            if(userNameField.text.length > 0 && passwordField.text.length > 0){
                // 延迟触发自动登录，因为QML文件可能还没准备好
                Js.setTimeout(Js.signIn, 800);
            }
        }
    }

    onClosing: {
        rootView.qsSignOut();

        Qt.quit();
    }
}
